﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using CE.Global;
using CE.Database;

public partial class HomeMasterPage : System.Web.UI.MasterPage
{
    string selectedLanguage;
    static int intHour, intMin, intSec, intExtend;
    static string strHour, strMin, strSec;

    static double TimeAllSecondes = 20;
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/SiteUserSecureLogin.aspx");
        }

        if (Request.UserAgent.IndexOf("AppleWebKit") > 0)
        {
            Request.Browser.Adapters.Clear();
        }

        #region Page Script Reference
        HyperLink _hplJS = new HyperLink();
        _hplJS.NavigateUrl = "~/css/main-home.css";
        Page.RegisterClientScriptBlock("MyScriptCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        //_hplJS.NavigateUrl = "~/css/colorbox.css";
        //Page.RegisterClientScriptBlock("MyScriptColorBoxCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        //_hplJS.NavigateUrl = "~/css/print.css";
        //Page.RegisterClientScriptBlock("MyScriptCss2", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "' media='print'/>");

        _hplJS.NavigateUrl = "~/css/graph.css";
        Page.RegisterClientScriptBlock("MyScriptGraphCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/js/datatables/css/jquery.dataTables.css";
        Page.RegisterClientScriptBlock("MyCss", "<link rel='stylesheet' type='text/css' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/favicon.png";
        Page.RegisterClientScriptBlock("MyScriptFavicon", "<link rel='shortcut icon' href='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'/>");

        _hplJS.NavigateUrl = "~/js/jquery.min.js";
        Page.RegisterClientScriptBlock("MyQueryMIN", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/colorbox/jquery.colorbox-min.js";
        Page.RegisterClientScriptBlock("MyQueryColorBox", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/jquery-ui.min.js";
        Page.RegisterClientScriptBlock("MyQueryUIMIN", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/carinsurance/renewal/magnific-popup/jquery.magnific-popup.js";
        Page.RegisterClientScriptBlock("MagnificPop", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/plugins/ui/jquery.collapsible.min.js";
        ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "MyScript12", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>", false);

        _hplJS.NavigateUrl = "~/js/jquery.flot.min.js";
        Page.RegisterClientScriptBlock("MyQueryGraphMIN", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/jquery.circliful.min.js";
        Page.RegisterClientScriptBlock("MyQueryCircleMIN", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        _hplJS.NavigateUrl = "~/js/jquery.final-countdown.js";
        Page.RegisterClientScriptBlock("MyQueryCountdown", "<script type='text/javascript' src='" + _hplJS.ResolveClientUrl(_hplJS.NavigateUrl) + "'></script>");

        #endregion

        if (!Page.IsPostBack)
        {
            btnLanguage.OnClientClick = "javascript:return confirm('" + Resources.CarInsurance.LanguageChangeMessage + "');";

            string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXCRMIsCXLive");
            if (_strCXLive == "0")
            {
                if (Session[CE.Global.General.Guest] == null)
                {
                    Response.Redirect("~/Default.aspx");
                }
            }

            if (Session[CE.Global.General.UserId] != null)
            {
                Page.Title = "GG-CRM - กล้วยๆ.com - Teledirect Thailand";
                lblLogInOut.Text = Resources.General.SignOut;
                hplChangePassword.Visible = true;
                hplLogInOut.NavigateUrl = "~/User/Logout.aspx";
                hplLogInOut.Attributes.Add("onclick", "javascript:return confirm('" + Resources.General.ConfirmSIGNOUTMessage + "')");
            }

            #region Agent Details
            if (Session[CE.Global.General.UserName] != null)
            {
                string AgentName = Session[CE.Global.General.UserName].ToString();
                if (AgentName.Length > 15)
                {
                    AgentName = AgentName.Substring(0, 13) + "..";
                }
                lblAgentName.Text = "Hi, " + AgentName;
            }

            DateTime dt = CE.Global.General.GetUTCDateTime();
            lblChallengeTittle.Text = "";
            lblChallengeTittle.Text = dt.ToString("MMM").ToUpper() + " " + dt.ToString("yyyy");
            #endregion

            #region Dashboard
            DateTime time = CE.Global.General.GetUTCDateTime();
            //lblTodayDateSmall.Text += "Dashboard (" + time.ToString("dddd MMMM dd, yyyy") + ")";
            //lblTodayDateSmall.Font.Bold = true;

            string _ciPendingCallBackCount = GetPendingCallBackCount();
            lblSideCIPendingCallCount.Text = "<strong>" + _ciPendingCallBackCount + "</strong>";
            Int32 _Pending = 0;
            if (_ciPendingCallBackCount != string.Empty)
            {
                if (!Int32.TryParse(_ciPendingCallBackCount, out _Pending))
                {
                    _Pending = 0;
                }
            }

            string _ciNormalCountbyAgent = GetCarDetailCountByAgent(Convert.ToInt64(Session[CE.Global.General.UserId].ToString()));
            Int32 _NewLead = 0;
            if (_ciNormalCountbyAgent != string.Empty)
            {
                if (!Int32.TryParse(_ciNormalCountbyAgent, out _NewLead))
                {
                    _NewLead = 0;
                }
            }


            Int32 TotalPending = 0;
            TotalPending = _Pending + _NewLead;

            int _PendingCallBackCount = 0;
            if (TotalPending > 0)
            {
                //PHYO
                lblSideCINormalCallCount.Text = "<strong>" + TotalPending.ToString() + "</strong>";
                if (Int32.TryParse(_ciPendingCallBackCount, out _PendingCallBackCount))
                {
                    if (_PendingCallBackCount > 0)
                    {
                        hplSideCINormalCall.BorderColor = System.Drawing.Color.Red;
                        //Yui@12Mar2015
                        //Timer1.Interval += 60 * 5 * 1000;
                    }
                }
            }

            if (Session[CE.Global.General.UserRole] != null)
            {
                if (Session[CE.Global.General.UserRole].ToString() == "201")
                {
                    //hplCINormal.Enabled = false;
                    //hplDashbook.Enabled = false;
                    hplFeedBack.Enabled = false;

                    //hplSideCICallDirect.Visible = false;
                    hplSideCINormalCall.Visible = false;
                    //hplPendingCall.Visible = false;

                    //lblCICallMeBackCount.Visible = false;
                    //lblCICallMeSmallCount.Visible = false;
                    //lblCINormalSmallCount.Visible = false;

                    //hplSearchCustomer.NavigateUrl = "~/carinsurance/ViewCICustomerCallNewSearch.aspx?page=oldquote";
                }

                //Yui@12Mar2015
                //if (Session[CE.Global.General.UserRole].ToString() != "100")
                //{
                //    Timer1.Interval = 24 * 60 * 60 * 1000;
                //    Timer1.Enabled = false;
                //}
                //else
                if (Session[CE.Global.General.UserRole].ToString() == "100")
                {
                    if (_PendingCallBackCount > 0)
                    {
                        if (Request.Url.PathAndQuery.ToLower().Contains("siteuserhome.aspx"))
                        {
                        }
                    }
                }
            }

            //[Yui] @ 08/08/2013 
            if (Session[CE.Global.General.IsRenewalAgent].ToString() == "1")
            {
                hplRenewalNotice.Visible = true;
            }
            GetSalesGropChallenge();
            #endregion

        }
    }

    private string GetCarDetailCount(int intCallType, Int64 AgentID)
    {
        string _retValue = "0";
        try
        {
            //[sprocCXCRMGetCarDetailCount]
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMGetCarDetailCount";
            SortedList _paraList = new SortedList();
            _paraList.Add("@CallType", intCallType);
            _paraList.Add("@LangOption", lblLangOption.Text);
            _paraList.Add("@AgentID", AgentID);
            object _objRetCount = _db.ExecuteScalarSP(_spName, _paraList);
            if (_objRetCount != null)
            {
                _retValue = _objRetCount.ToString();
            }
        }
        catch (Exception Ex)
        {
            _retValue = "N/A";
        }
        return _retValue;
    }

    private string GetCarDetailCountByAgent(Int64 AgentId)
    {
        string _retValue = "0";
        try
        {
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCXCRMGetCarDetailCountByAgentId";
            SortedList _paraList = new SortedList();
            _paraList.Add("@AgentId", AgentId);
            object _objRetCount = _db.ExecuteScalarSP(_spName, _paraList);
            if (_objRetCount != null)
            {
                _retValue = _objRetCount.ToString();
            }
        }
        catch (Exception Ex)
        {
            _retValue = "N/A";
        }
        return _retValue;
    }

    private string GetPendingCallBackCount()
    {
        string _retValue = "0";
        try
        {
            if (Session[CE.Global.General.UserId] != null)
            {
                MyDatabase _db = new MyDatabase();
                string _spName = "sprocCXCRMGetPendingCallBackCount";
                SortedList _paraList = new SortedList();
                _paraList.Add("@AgentId", Session[CE.Global.General.UserId].ToString());
                object _objRetCount = _db.ExecuteScalarSP(_spName, _paraList);
                if (_objRetCount != null)
                {
                    _retValue = _objRetCount.ToString();
                }
            }
        }
        catch (Exception Ex)
        {
        }
        return _retValue;
    }

    protected void btnLanguage_Click(object sender, EventArgs e)
    {
        selectedLanguage = string.Empty;

        if (lblLanguage.Text != string.Empty)
        {
            Session["Language"] = lblLanguage.Text;
        }

        if (Session["Language"] != null)
        {
            selectedLanguage = Session["Language"].ToString();
        }
        else
        {
            selectedLanguage = "th-TH";
            Session["Language"] = selectedLanguage;
        }

        if (selectedLanguage == "th-TH")
        {
            Session["Language"] = "en-US";
        }
        else
        {
            Session["Language"] = "th-TH";
        }

        lblLanguage.Text = selectedLanguage;

        Response.Redirect(Request.UrlReferrer.AbsoluteUri);
    }

    protected void UpdatePendingStatus(object sender, EventArgs e)
    {
        try
        {
            //string _ciStrCallBackCount = GetPendingCallBackCount();
            //int _ciIntCallBackCount = 0;
            //if (Int32.TryParse(_ciStrCallBackCount, out _ciIntCallBackCount))
            //{
                //if (_ciIntCallBackCount > 0)
                //{
                    //hplSideCINormalCall.BorderColor = System.Drawing.Color.Red;
                    //ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), Guid.NewGuid().ToString(), "alert('[หมดเวลา!]โปรดตรวจสอบตารางเวลาการโทรกลับ.\\nคุณมีตารางโทรกลับ  " + _ciIntCallBackCount.ToString() + " รายการ\\n\\n[Time Up!] Please check call back schedule.\\nYou got (" + _ciIntCallBackCount.ToString() + ") call back schedule(s) due.');", true);
                    //Timer1.Interval += 60 * 5 * 1000;
                //}
            //}
        }
        catch (Exception Ex)
        {
            Ex.ToString();
        }
    }

    private void GetSalesGropChallenge()
    {
        try
        {
            #region Variable Declaration
            string _spName = "";
            Int32 _RankBy = 0;

            string _month = General.GetUTCDateTime().ToString("MMM");
            float _Target = 0.0f;
            float _Collection = 0.0f;
            float _Balance = 0.0f;
            Int16 _Rank = 0;
            float _targetper = 0.0f;
            float _salesper = 0.0f;
            Int32 _TotalWorkingDays = 0;
            Int32 _RemainWorkingDays = 0;
            Int32 _PastWorkingDays = 0;
            float _DailyTarget = 0.0f;
            float _TodayTarget = 0.0f;
            float _TodayCollection = 0.0f;

            int targetpercent = 0;
            string targetpercentdisplay = targetpercent.ToString() + "%";
            string targetcolor = "";
            int salepercent = 0;
            string salepercentdisplay = salepercent.ToString() + "%";
            string salecolor = "";
            #endregion

            MyDatabase _db = new MyDatabase();
            DataSet _dsAmount = new DataSet();

            if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100")
            {
                Int64 intLoginUserId = 0;
                if (Session[General.UserId] != null)
                {
                    if (!Int64.TryParse(Session[General.UserId].ToString(), out intLoginUserId))
                    {
                        intLoginUserId = 0;
                    }
                }
                lblChallengeIndividual.Text = Resources.General.ChallengeIndividual;
                _spName = "sprocCXCRMAgentSalesRankSelectedItemList";
                SortedList _paraList = new SortedList();
                _paraList.Add("@LoginAgentId", intLoginUserId);
                //_paraList.Add("@LoginAgentId", "59141");
                _dsAmount = _db.GetDataSet(_spName, _paraList);

                #region Datatable Values
                DataTable _dtAgentSales = new DataTable();
                if (_dsAmount != null && _dsAmount.Tables.Count > 0)
                {
                    if (_dsAmount.Tables[0].Rows.Count > 0)
                    {
                        _RankBy = _dsAmount.Tables[0].Rows.Count;
                    }
                    if (_dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
                    {
                        _dtAgentSales = _dsAmount.Tables[0];
                    }
                }
                //if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
                //{
                //    if (_dsAmount != null && _dsAmount.Tables.Count > 0)
                //    {
                //        if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
                //        {
                //            _dtAgentSales = _dsAmount.Tables[1];
                //            _RankBy = _dsAmount.Tables[1].Rows.Count;
                //        }
                //    }
                //}
                //else
                //{
                //    if (_dsAmount != null && _dsAmount.Tables.Count > 0)
                //    {
                //        if (_dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
                //        {
                //            _dtAgentSales = _dsAmount.Tables[0];
                //            _RankBy = _dsAmount.Tables[0].Rows.Count;
                //        }
                //    }
                //}
                #endregion

                #region Collection & Ranking
                if (_dsAmount != null && _dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
                {
                    foreach (DataRow row in _dsAmount.Tables[3].Rows)
                    {
                        if (row["NameofMonth"].ToString().ToUpper() == _month.ToUpper())
                        {
                            _Target = Convert.ToSingle(row["TargetAmount"].ToString());
                        }
                    }
                }
                //if (_dsAmount != null && _dsAmount.Tables.Count > 2 && _dsAmount.Tables[2].Rows.Count > 0)
                //{
                //    foreach (DataRow row in _dsAmount.Tables[2].Rows)
                //    {
                //        foreach (DataColumn dc in _dsAmount.Tables[2].Columns)
                //        {
                //            string MonthCol = dc.ColumnName;
                //            if (MonthCol == _month)
                //            {
                //                _Target = Convert.ToSingle(row[dc].ToString());
                //            }
                //        }
                //    }
                //}
                if (_dsAmount != null && _dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
                {
                    _Collection = Convert.ToSingle(_dsAmount.Tables[1].Rows[0]["IndividualAmount"].ToString());
                    _Rank = Convert.ToInt16(_dsAmount.Tables[1].Rows[0]["AgentRank"].ToString());
                    _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[1].Rows[0]["TotalWorkingDays"].ToString());
                    _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[1].Rows[0]["RemainWorkingDays"].ToString());
                    _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
                }
                //if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
                //{
                //    if (_dsAmount != null && _dsAmount.Tables.Count > 4 && _dsAmount.Tables[4].Rows.Count > 0)
                //    {
                //        _Collection = Convert.ToSingle(_dsAmount.Tables[4].Rows[0]["IndividualAmount"].ToString());
                //        _Rank = Convert.ToInt16(_dsAmount.Tables[4].Rows[0]["AgentRank"].ToString());
                //        _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["TotalWorkingDays"].ToString());
                //        _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["RemainWorkingDays"].ToString());
                //        _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
                //    }
                //}
                //else
                //{
                //    if (_dsAmount != null && _dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
                //    {
                //        _Collection = Convert.ToSingle(_dsAmount.Tables[3].Rows[0]["IndividualAmount"].ToString());
                //        _Rank = Convert.ToInt16(_dsAmount.Tables[3].Rows[0]["AgentRank"].ToString());
                //        _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[3].Rows[0]["TotalWorkingDays"].ToString());
                //        _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[3].Rows[0]["RemainWorkingDays"].ToString());
                //        _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
                //    }
                //}
                if (_dsAmount != null && _dsAmount.Tables.Count > 2 && _dsAmount.Tables[2].Rows.Count > 0)
                {
                    if (!float.TryParse(_dsAmount.Tables[2].Rows[0]["TodayCollection"].ToString(), out _TodayCollection))
                    {
                        _TodayCollection = 0.0f;
                    }
                }
                //if (_dsAmount != null && _dsAmount.Tables.Count > 5 && _dsAmount.Tables[5].Rows.Count > 0)
                //{
                //    if (!float.TryParse(_dsAmount.Tables[5].Rows[0]["TodayCollection"].ToString(), out _TodayCollection))
                //    {
                //        _TodayCollection = 0.0f;
                //    }
                //}

                if (_Collection > 0)
                {
                    _Balance = Convert.ToSingle(_Target - _Collection);
                }

                lblChallengeIndividualAmount.Text = Math.Floor(_Collection).ToString("#,000.00");
                lblChallengeBalanceTarget.Text = Math.Ceiling(_Balance).ToString("#,000.00");
                lblChallengeRanking.Text = _Rank.ToString() + "/" + _RankBy.ToString();
                imgHand.Visible = false;

                if (_dtAgentSales.Rows.Count > 0)
                {
                    int _firstRank = 1;
                    int _secondRank = 2;
                    int _thirdRank = 3;
                    int _LastRank = _dtAgentSales.Rows.Count;
                    float _FirstRankAmount = 0.0f;
                    float _SecondRankAmount = 0.0f;
                    float _ThirdRankAmount = 0.0f;
                    float _LastRankAmount = 0.0f;
                    bool _isfirst = false;
                    bool _islast = false;
                    if (!float.TryParse(_dtAgentSales.Rows[0]["IndividualAmount"].ToString(), out _FirstRankAmount))
                    {
                        _FirstRankAmount = 0.0f;
                    }
                    if (!float.TryParse(_dtAgentSales.Rows[1]["IndividualAmount"].ToString(), out _SecondRankAmount))
                    {
                        _SecondRankAmount = 0.0f;
                    }
                    if (!float.TryParse(_dtAgentSales.Rows[2]["IndividualAmount"].ToString(), out _ThirdRankAmount))
                    {
                        _ThirdRankAmount = 0.0f;
                    }
                    if (!float.TryParse(_dtAgentSales.Rows[_dtAgentSales.Rows.Count - 1]["IndividualAmount"].ToString(), out _LastRankAmount))
                    {
                        _LastRankAmount = 0.0f;
                    }

                    lblFirstRank.Text = _firstRank.ToString();
                    lblFirstRankAmount.Text = Math.Floor(_FirstRankAmount).ToString("#,000.00");
                    lblSecondRank.Text = _secondRank.ToString();
                    lblSecondRankAmount.Text = Math.Floor(_SecondRankAmount).ToString("#,000.00");
                    lblThirdRank.Text = _thirdRank.ToString();
                    lblThirdRankAmount.Text = Math.Floor(_ThirdRankAmount).ToString("#,000.00");
                    lblLastRank.Text = _LastRank.ToString();
                    lblLastRankAmount.Text = Math.Floor(_LastRankAmount).ToString("#,000.00");


                    _DailyTarget = _Target / _TotalWorkingDays;
                    _TodayTarget = _DailyTarget;
                    if (_Target > 0 && _Collection > 0)
                    {
                        _targetper = (_Collection / _Target) * 100;
                        _TodayTarget = (_Target - _Collection) / _RemainWorkingDays;
                    }

                    if (_TodayTarget > 0 && _TodayCollection > 0)
                    {
                        _salesper = (_TodayCollection / _TodayTarget) * 100;
                    }

                    #region Sales & Target - Values
                    targetpercent = Convert.ToInt32(_targetper);
                    targetpercentdisplay = targetpercent.ToString() + "%";
                    targetcolor = "";
                    salepercent = Convert.ToInt32(_salesper);
                    salepercentdisplay = salepercent.ToString() + "%";
                    salecolor = "";

                    //Target
                    if (targetpercent <= 25)
                    {
                        targetcolor = "tx-amount tx-red";
                    }
                    else if (targetpercent > 25 && targetpercent <= 50)
                    {
                        targetcolor = "tx-amount tx-orange";
                    }
                    else if (targetpercent > 50 && targetpercent <= 75)
                    {
                        targetcolor = "tx-amount tx-yellow";
                    }
                    else if (targetpercent > 75)
                    {
                        targetcolor = "tx-amount tx-green";
                    }

                    divRankBy.Attributes.Add("class", targetcolor);
                    #endregion
                }
                #endregion
            }
            else if (Session[General.UserRole] != null && Session[General.UserRole].ToString() != "100")
            {
                lblChallengeIndividual.Text = Resources.General.TotalCollectedAmount;
                _spName = "sprocCXCRMAgentSalesTargetSelectedItemList";
                SortedList _paraList = new SortedList();
                _paraList.Add("@LoginType", "2");
                _dsAmount = _db.GetDataSet(_spName, _paraList);

                #region Datatable Values
                DataTable _dtAgentSales = new DataTable();
                if (_dsAmount != null && _dsAmount.Tables.Count > 2)
                {
                    if (_dsAmount.Tables[2].Rows.Count > 0)
                    {
                        _dtAgentSales = _dsAmount.Tables[2];
                        _RankBy = _dsAmount.Tables[2].Rows.Count;
                    }
                }
                #endregion

                #region Collection & Ranking
                if (_dsAmount != null && _dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
                {
                    foreach (DataRow row in _dsAmount.Tables[3].Rows)
                    {
                        foreach (DataColumn dc in _dsAmount.Tables[3].Columns)
                        {
                            string MonthCol = dc.ColumnName;
                            if (MonthCol == _month)
                            {
                                _Target = Convert.ToSingle(row[dc].ToString());
                            }
                        }
                    }
                }

                if (_dsAmount != null && _dsAmount.Tables.Count > 4 && _dsAmount.Tables[4].Rows.Count > 0)
                {
                    _Collection = Convert.ToSingle(_dsAmount.Tables[4].Rows[0]["IndividualAmount"].ToString());
                    _Rank = Convert.ToInt16(_dsAmount.Tables[4].Rows[0]["AgentRank"].ToString());
                    _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["TotalWorkingDays"].ToString());
                    _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["RemainWorkingDays"].ToString());
                    _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
                }


                if (_dsAmount != null && _dsAmount.Tables.Count > 5 && _dsAmount.Tables[5].Rows.Count > 0)
                {
                    if (!float.TryParse(_dsAmount.Tables[5].Rows[0]["TodayCollection"].ToString(), out _TodayCollection))
                    {
                        _TodayCollection = 0.0f;
                    }
                }

                if (_Collection > 0)
                {
                    _Balance = Convert.ToSingle(_Target - _Collection);
                }

                lblChallengeIndividualAmount.Text = Math.Floor(_Collection).ToString("#,000.00");
                lblChallengeBalanceTarget.Text = Math.Ceiling(_Balance).ToString("#,000.00");
                lblChallengeRanking.Text = "";// _Rank.ToString() + "/" + _RankBy.ToString();
                imgHand.Visible = true;

                if (_dtAgentSales.Rows.Count > 0)
                {
                    int _firstRank = 1;
                    int _secondRank = 2;
                    int _thirdRank = 3;
                    int _LastRank = _dtAgentSales.Rows.Count;
                    float _FirstRankAmount = 0.0f;
                    float _SecondRankAmount = 0.0f;
                    float _ThirdRankAmount = 0.0f;
                    float _LastRankAmount = 0.0f;
                    bool _isfirst = false;
                    bool _islast = false;
                    if (!float.TryParse(_dtAgentSales.Rows[0]["IndividualAmount"].ToString(), out _FirstRankAmount))
                    {
                        _FirstRankAmount = 0.0f;
                    }
                    if (!float.TryParse(_dtAgentSales.Rows[1]["IndividualAmount"].ToString(), out _SecondRankAmount))
                    {
                        _SecondRankAmount = 0.0f;
                    }
                    if (!float.TryParse(_dtAgentSales.Rows[2]["IndividualAmount"].ToString(), out _ThirdRankAmount))
                    {
                        _ThirdRankAmount = 0.0f;
                    }
                    if (!float.TryParse(_dtAgentSales.Rows[_dtAgentSales.Rows.Count - 1]["IndividualAmount"].ToString(), out _LastRankAmount))
                    {
                        _LastRankAmount = 0.0f;
                    }

                    lblFirstRank.Text = _firstRank.ToString();
                    lblFirstRankAmount.Text = Math.Floor(_FirstRankAmount).ToString("#,000.00");
                    lblSecondRank.Text = _secondRank.ToString();
                    lblSecondRankAmount.Text = Math.Floor(_SecondRankAmount).ToString("#,000.00");
                    lblThirdRank.Text = _thirdRank.ToString();
                    lblThirdRankAmount.Text = Math.Floor(_ThirdRankAmount).ToString("#,000.00");
                    lblLastRank.Text = _LastRank.ToString();
                    lblLastRankAmount.Text = Math.Floor(_LastRankAmount).ToString("#,000.00");


                    _DailyTarget = _Target / _TotalWorkingDays;
                    _TodayTarget = _DailyTarget;
                    if (_Target > 0 && _Collection > 0)
                    {
                        _targetper = (_Collection / _Target) * 100;
                        _TodayTarget = (_Target - _Collection) / _RemainWorkingDays;
                    }

                    if (_TodayTarget > 0 && _TodayCollection > 0)
                    {
                        _salesper = (_TodayCollection / _TodayTarget) * 100;
                    }

                    #region Sales & Target - Values
                    targetpercent = Convert.ToInt32(_targetper);
                    targetpercentdisplay = targetpercent.ToString() + "%";
                    targetcolor = "";
                    salepercent = Convert.ToInt32(_salesper);
                    salepercentdisplay = salepercent.ToString() + "%";
                    salecolor = "";

                    //Target
                    if (targetpercent <= 25)
                    {
                        targetcolor = "tx-amount tx-red";
                    }
                    else if (targetpercent > 25 && targetpercent <= 50)
                    {
                        targetcolor = "tx-amount tx-orange";
                    }
                    else if (targetpercent > 50 && targetpercent <= 75)
                    {
                        targetcolor = "tx-amount tx-yellow";
                    }
                    else if (targetpercent > 75)
                    {
                        targetcolor = "tx-amount tx-green";
                    }

                    divRankBy.Attributes.Add("class", targetcolor);
                    #endregion
                }
                #endregion
            }

        }
        catch (Exception ex)
        {
        }
        finally
        {

        }
    }

    #region Not Using

    //private void GetSalesGropChallengeOld()
    //{
    //    Int32 _RankBy = 0;
    //    MyDatabase _db = new MyDatabase("GGR_DBConnectionString_TEST");
    //    string _spName = "sprocCXCRMAgentSalesRankSelectedItemList";
    //    DataSet _dsAmount = new DataSet();
    //    if (Session[General.UserRole] != null)
    //    //if (Session[General.UserRole] != null && Session[General.UserRole].ToString() == "100")
    //    {
    //        SortedList _paraList = new SortedList();
    //        _paraList.Add("@LoginAgentId", "59141");
    //        //_paraList.Add("@LoginAgentId", Session[CE.Global.General.UserId].ToString());
    //        _dsAmount = _db.GetDataSet(_spName, _paraList);
    //    }
    //    else
    //    {
    //        _dsAmount = _db.GetDataSet(_spName);
    //    }

    //    #region Chart Values
    //    DataTable _dtAgentSales = new DataTable();
    //    if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
    //    {
    //        if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //        {
    //            if (_dsAmount.Tables.Count > 1 && _dsAmount.Tables[1].Rows.Count > 0)
    //            {
    //                _dtAgentSales = _dsAmount.Tables[1];
    //                _RankBy = _dsAmount.Tables[1].Rows.Count;
    //            }
    //        }
    //    }
    //    else
    //    {
    //        if (_dsAmount != null && _dsAmount.Tables.Count > 0)
    //        {
    //            if (_dsAmount.Tables.Count > 0 && _dsAmount.Tables[0].Rows.Count > 0)
    //            {
    //                _dtAgentSales = _dsAmount.Tables[0];
    //                _RankBy = _dsAmount.Tables[0].Rows.Count;
    //            }
    //        }
    //    }
    //    #endregion

    //    #region Collection & Ranking

    //    string _month = General.GetUTCDateTime().ToString("MMM");
    //    float _Target = 0.0f;
    //    float _Collection = 0.0f;
    //    float _Balance = 0.0f;
    //    Int16 _Rank = 0;
    //    float _targetper = 0.0f;
    //    float _salesper = 0.0f;
    //    Int32 _TotalWorkingDays = 0;
    //    Int32 _RemainWorkingDays = 0;
    //    Int32 _PastWorkingDays = 0;
    //    float _DailyTarget = 0.0f;
    //    float _TodayTarget = 0.0f;
    //    float _TodayCollection = 0.0f;

    //    if (_dsAmount != null && _dsAmount.Tables.Count > 2 && _dsAmount.Tables[2].Rows.Count > 0)
    //    {
    //        foreach (DataRow row in _dsAmount.Tables[2].Rows)
    //        {
    //            foreach (DataColumn dc in _dsAmount.Tables[2].Columns)
    //            {
    //                string MonthCol = dc.ColumnName;
    //                if (MonthCol == _month)
    //                {
    //                    _Target = Convert.ToSingle(row[dc].ToString());
    //                }
    //            }
    //        }
    //    }
    //    if (Session[General.IsRenewalAgent] != null && Session[General.IsRenewalAgent].ToString() == "1")
    //    {
    //        if (_dsAmount != null && _dsAmount.Tables.Count > 4 && _dsAmount.Tables[4].Rows.Count > 0)
    //        {
    //            _Collection = Convert.ToSingle(_dsAmount.Tables[4].Rows[0]["IndividualAmount"].ToString());
    //            _Rank = Convert.ToInt16(_dsAmount.Tables[4].Rows[0]["AgentRank"].ToString());
    //            _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["TotalWorkingDays"].ToString());
    //            _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[4].Rows[0]["RemainWorkingDays"].ToString());
    //            _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
    //        }
    //    }
    //    else
    //    {
    //        if (_dsAmount != null && _dsAmount.Tables.Count > 3 && _dsAmount.Tables[3].Rows.Count > 0)
    //        {
    //            _Collection = Convert.ToSingle(_dsAmount.Tables[3].Rows[0]["IndividualAmount"].ToString());
    //            _Rank = Convert.ToInt16(_dsAmount.Tables[3].Rows[0]["AgentRank"].ToString());
    //            _TotalWorkingDays = Convert.ToInt32(_dsAmount.Tables[3].Rows[0]["TotalWorkingDays"].ToString());
    //            _RemainWorkingDays = Convert.ToInt32(_dsAmount.Tables[3].Rows[0]["RemainWorkingDays"].ToString());
    //            _PastWorkingDays = _TotalWorkingDays - _RemainWorkingDays;
    //        }
    //    }
    //    if (_dsAmount != null && _dsAmount.Tables.Count > 5 && _dsAmount.Tables[5].Rows.Count > 0)
    //    {
    //        if (!float.TryParse(_dsAmount.Tables[5].Rows[0]["TodayCollection"].ToString(), out _TodayCollection))
    //        {
    //            _TodayCollection = 0.0f;
    //        }
    //    }

    //    if (_Collection > 0)
    //    {
    //        _Balance = Convert.ToSingle(_Target - _Collection);
    //    }

    //    lblChallengeIndividualAmount.Text = Math.Floor(_Collection).ToString("#,000.00");
    //    lblChallengeBalanceTarget.Text = Math.Ceiling(_Balance).ToString("#,000.00");
    //    //lblChallengeIndividualAmount.Text = _Collection.ToString("#,000.00");
    //    //lblChallengeBalanceTarget.Text = _Balance.ToString("#,000.00");
    //    lblChallengeRanking.Text = _Rank.ToString() + "/" + _RankBy.ToString();

    //    if (_dtAgentSales.Rows.Count > 0)
    //    {
    //        int _firstRank = 1;
    //        int _LastRank = _dtAgentSales.Rows.Count;
    //        float _FirstRankAmount = 0.0f;
    //        float _LastRankAmount = 0.0f;
    //        bool _isfirst = false;
    //        bool _islast = false;
    //        if (!float.TryParse(_dtAgentSales.Rows[0]["IndividualAmount"].ToString(), out _FirstRankAmount))
    //        {
    //            _FirstRankAmount = 0.0f;
    //        }
    //        if (!float.TryParse(_dtAgentSales.Rows[_dtAgentSales.Rows.Count - 1]["IndividualAmount"].ToString(), out _LastRankAmount))
    //        {
    //            _LastRankAmount = 0.0f;
    //        }

    //        lblFirstRank.Text = _firstRank.ToString();
    //        //lblFirstRankAmount.Text = _FirstRankAmount.ToString("#,000.00");
    //        lblFirstRankAmount.Text = Math.Floor(_FirstRankAmount).ToString("#,000.00");
    //        lblLastRank.Text = _LastRank.ToString();
    //        //lblLastRankAmount.Text = _LastRankAmount.ToString("#,000.00");
    //        lblLastRankAmount.Text = Math.Floor(_LastRankAmount).ToString("#,000.00");


    //        _DailyTarget = _Target / _TotalWorkingDays;
    //        _TodayTarget = _DailyTarget;
    //        if (_Target > 0 && _Collection > 0)
    //        {
    //            _targetper = (_Collection / _Target) * 100;
    //            _TodayTarget = (_Target - _Collection) / _RemainWorkingDays;
    //        }

    //        if (_TodayTarget > 0 && _TodayCollection > 0)
    //        {
    //            _salesper = (_TodayCollection / _TodayTarget) * 100;
    //        }
    //        #region Sales & Target - Values
    //        int targetpercent = Convert.ToInt32(_targetper);
    //        string targetpercentdisplay = targetpercent.ToString() + "%";
    //        string targetcolor = "";
    //        int salepercent = Convert.ToInt32(_salesper);
    //        string salepercentdisplay = salepercent.ToString() + "%";
    //        string salecolor = "";

    //        //Target
    //        if (targetpercent <= 25)
    //        {
    //            targetcolor = "tx-amount tx-red";
    //        }
    //        else if (targetpercent > 25 && targetpercent <= 50)
    //        {
    //            targetcolor = "tx-amount tx-orange";
    //        }
    //        else if (targetpercent > 50 && targetpercent <= 75)
    //        {
    //            targetcolor = "tx-amount tx-yellow";
    //        }
    //        else if (targetpercent > 75)
    //        {
    //            targetcolor = "tx-amount tx-green";
    //        }

    //        divRankBy.Attributes.Add("class", targetcolor);
    //    }
    //        #endregion
    //    #endregion
    //}

    #endregion

}
